﻿namespace ChessToys.ChessController.Cli
{
    using System;
    using System.Reflection;
    using ChessToys.ChessController;

    public class Arguments
    {
        public static Arguments Parse(String[] args)
        {
            // defaults
            IChessController whiteController = new HumanConsoleChessController();
            IChessController blackController = null;
            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                    case "-white":
                        whiteController = ParseController(args, i, "-white");
                        i++;
                        break;
                    case "-black":
                        blackController = ParseController(args, i, "-black");
                        i++;
                        break;
                    case "-help":
                        Console.WriteLine("ChessToys.ChessController.Cli options:");
                        Console.WriteLine("  -help                              prints this message");
                        Console.WriteLine("  -version                           prints version informations");
                        Console.WriteLine("  -white [Assembly],[Controller]     use the specified controller from the given assembly to control white (default is)");
                        Console.WriteLine("  -black [Assembly],[Controller]     use the specified controller from the given assembly to control black");
                        return null;
                    case "-version":
                        Console.WriteLine("ChessToys.ChessController.Cli version 1.0.0-SNAPSHOT");
                        return null;
                }
            }
            return new Arguments(whiteController, blackController);
        }

        private static IChessController ParseController(String[] args, int i, String option)
        {
            if (i + 1 >= args.Length)
            {
                Console.WriteLine("Missing argument for \"{0}\".", option);
                return null;
            }
            String[] splitted = args[i + 1].Split(',');
            try
            {
                Assembly controllerAssembly = Assembly.LoadFrom(splitted[0]);
                IChessController controller = controllerAssembly.CreateInstance(splitted[1]) as IChessController;
                if (controller == null)
                {
                    Console.WriteLine(String.Format("Unable to instantiate {0} for \"{1}\".", splitted[1], option));
                }
                return controller;
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Unable to load assembly {0} for \"{1}\".", splitted[0], option));
                return null;
            }
        }

        private IChessController _whiteController;
        private IChessController _blackController;

        private Arguments(IChessController whiteController, IChessController blackController)
        {
            _whiteController = whiteController;
            _blackController = blackController;
        }

        public IChessController WhiteController
        {
            get { return _whiteController; }
        }

        public IChessController BlackController
        {
            get { return _blackController; }
        }
    }
}
